﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using ProgNetDxComponents.ExtenderProviders.Language;

namespace ProgNetDxComponents.Controls.Editors
{
    //public partial class MultiEditTreeControl : UserControl
    public partial class MultiEditTreeControl : MultiEditBaseTab
    {
        public MultiEditTreeControl()
        {
            InitializeComponent();
            treeMain.DataSource = DataView;
        }

        public override DataTable DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                base.DataSource = value;
                treeMain.Columns.Clear();
                int idx = 0;
                if (treeMain.Columns.Count == 0)
                {
                    if (value != null)
                    {
                        foreach (DataColumn col in value.Columns)
                        {
                            TreeListColumn c = treeMain.Columns.Add();
                            c.FieldName = col.ColumnName;
                            if (this.IsAppRunning())
                            {
                                string caption = ProgNetLanguage.sGetString(col.ColumnName);
                                if (caption != ProgNetLanguage.NoEntryString)
                                    c.Caption = caption;
                            }
                            if (col.ColumnName == ParentFieldName)
                                c.Visible = false;
                            else
                            {
                                c.Visible = true;
                                c.VisibleIndex = idx;
                                idx++;
                            }
                        }
                    }
                }
            }
        }
        
        public string ParentFieldName
        {
            get { return treeMain.ParentFieldName; }
            set { treeMain.ParentFieldName = value; }
        }
        public override string ValueMember
        {
            get
            {
                return base.ValueMember;
            }
            set
            {
                base.ValueMember = value;
                treeMain.KeyFieldName = value;
            }
        }
        
        public TreeListColumnCollection Columns
        {
            get { return treeMain.Columns; }
        }
        
        private void treeMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                OnRowSelected();
            }
        }

        private void treeMain_DoubleClick(object sender, EventArgs e)
        {
            OnRowSelected();
        }

        public override DataRow GetSelectedRow()
        {
            if (treeMain.FocusedNode != null)
            {
                DataRowView drv = treeMain.GetDataRecordByNode(treeMain.FocusedNode) as DataRowView;
                if (drv != null)
                    return drv.Row;
            }
            return null;
        }
        
        public override void FocusValue(object editValue)
        {
            treeMain.CollapseAll();
            if (editValue != null)
            {
                TreeListNode node = treeMain.FindNodeByKeyID(editValue);
                if (node != null)
                {
                    treeMain.FocusedNode = node;
                }
            }
            else
            {
                treeMain.MoveFirst();
            }
        }
    }
}
